﻿/*
Copyright 2009 Matías Fidemraizer (www.matiasfidemraizer.com).

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

using System;
using System.Linq;
using System.Resources;
using System.Collections.Generic;

#region "Custom namespace declarations"
using Comkarl.Architecture.Patterns;
using Comkarl.Architecture.Patterns.MVC.Entities;
#endregion

namespace Comkarl.Architecture.Patterns.MVC
{
    /// <summary>
    /// Represents an instance of an MVC implementation.
    /// </summary>
    /// <typeparam name="TIdentifierName">
    /// The type for the identifier's name.
    /// </typeparam>
    public abstract class MVCInstance<TIdentifierName> : PatternInstance<TIdentifierName> 
        where TIdentifierName : IEquatable<TIdentifierName>
    {
        #region "Fields"
        /// <summary>
        /// Retrieves the list of current instance's controllers.
        /// </summary>
        private readonly List<Controller<TIdentifierName>> controllers;

        #endregion

        #region "Constructors"
        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="id">
        /// The identifier for the instance.
        /// </param>
        public MVCInstance(PatternInstanceIdentifier<TIdentifierName> id)
            : base(id)
        {
            controllers = new List<Controller<TIdentifierName>>();
        }
        #endregion

        #region "Properties"
        /// <summary>
        /// Gets the current instance's controllers.
        /// </summary>
        public List<Controller<TIdentifierName>> Controllers
        {
            get
            {
                return controllers;
            }
        }
        #endregion

        #region Methods
        /// <summary>
        /// Gets an associated controller providing its type.
        /// </summary>
        /// <typeparam name="TController">
        /// The type for the controller.
        /// </typeparam>
        /// <returns>
        /// The desired controller.
        /// </returns>
        public TController GetController<TController>()
            where TController : Controller<TIdentifierName>
        {
            return (TController)Controllers
                                    .Where<Controller<TIdentifierName>>(some => some.GetType() == typeof(TController))
                                        .First<Controller<TIdentifierName>>();
        }

        /// <summary>
        /// Gets an associated controller by its identifier.
        /// </summary>
        /// <param name="id">
        /// The identifier for the desired controller.
        /// </param>
        /// <returns>
        /// The desired controller.
        /// </returns>
        public Controller<TIdentifierName> GetControllerByID(Identifier<TIdentifierName> id)
        {
            return Controllers
                        .Where<Controller<TIdentifierName>>(some => some.ID == id)
                            .First<Controller<TIdentifierName>>();
        }
        #endregion
    }
}
